//
//  LTConversation.m
//  LTIM
//
//  Created by 俊松何 on 2017/3/27.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTConversation.h"
#import "LTDBHelper.h"
#import "LTTranslateModel.h"
#import "LTError.h"
#import <MJExtension/MJExtension.h>
@implementation LTConversation

-(void)setExtra:(NSString *)extra
{
    _extra = extra;
    if(extra != nil)
    {
        _extModel = [LTTranslateModel mj_objectWithKeyValues:extra];
    }else
    {
        _extModel = nil;
    }

}


-(void)insertMessage:(LTMessage *)aMessage
               error:(NSError **)error
{
    aMessage.conversationId = self.conversationId;
}

-(NSArray<LTMessage *> *)loadAllMessage
{
 return  [[LTDBHelper sharedbhelper] getMessageWithConversation:self.conversationId];

}

/*!
 *  \~chinese
 *  将消息设置为已读
 *
 *  @param aMessageId   要设置消息的ID
 *  @param pError       错误信息
 *
 */
- (BOOL)markMessageAsReadWithId:(NSString *)aMessageId
                          error:(LTError **)pError
{
  return   [[LTDBHelper sharedbhelper] markMessagesAsReadWithMessageId:aMessageId];
}

- (void)loadMessagesStarttime:(double)time
                        count:(int)aCount
              searchDirection:(LTMessageSearchDirection)aDirection
                   completion:(void (^)(NSArray<LTMessage *> *aMessages, LTError *aError))aCompletionBlock;
{
    
    
    [[LTDBHelper sharedbhelper] getMessageWithConversation:self.conversationId startTime:time count:aCount completion:aCompletionBlock];
}

- (void)loadMessagesStartMessageId:(NSString *)messageId
                             count:(int)aCount
                   searchDirection:(LTMessageSearchDirection)aDirection
                        completion:(void (^)(NSArray<LTMessage *> *aMessages, LTError *aError))aCompletionBlock
{
    [[LTDBHelper sharedbhelper] getmessageWithConversation:self.conversationId messageId:messageId count:aCount completion:aCompletionBlock];
}


-(void)markAllMessagesAsRead
{
    [[LTDBHelper sharedbhelper] markAllMessagesAsReadWithConversationID:self.conversationId];

}

-(NSInteger)unReadCount
{
 return  [[LTDBHelper sharedbhelper] getUnreadMessageWithConversationID:self.conversationId];
    
}
-(BOOL)haveAtMe
{
    NSArray *messageArray =  [[LTDBHelper sharedbhelper] getAtUnReadMessageConversationID:self.conversationId];
    
    for (LTMessage *message in messageArray) {
        LTMessageAtTextBody *body = (LTMessageAtTextBody *)message.body;
        if([body atHaveMe])
        {
            return YES;
        }
    }
    return NO;
}


-(LTMessage *)lastMessage
{
    return [[LTDBHelper sharedbhelper] getlastMessageWithConversationID:self.conversationId];
}

- (BOOL)markConversationisTop:(BOOL)istop
{
    
    _isTop = istop;
    return [[LTDBHelper sharedbhelper] markConversationID:self.conversationId isTop:istop];

}


- (BOOL)markConversationisSilent:(BOOL)isSilent
{
    _isSilent = isSilent;
    return [[LTDBHelper sharedbhelper] markConversationID:self.conversationId isSilent:isSilent];

 return NO;
}

- (BOOL)deleteAllMessage
{
   return [[LTDBHelper sharedbhelper] deleteMessageWithConversation:self.conversationId];
}

- (BOOL)deleteAllMessageWithMessageId:(NSString *)messageId
{
    return [[LTDBHelper sharedbhelper] deleteMessageWithMessageId:messageId];
}




-(BOOL)updateConversationToDB
{
  return  [[LTDBHelper sharedbhelper] AddConversation:self];
}

@end
